package cn.gson.springboot.model.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.ApplyDAO;
import cn.gson.springboot.model.dao.ApplyDetailDAO;
import cn.gson.springboot.model.dao.CkDAO;
import cn.gson.springboot.model.dao.DrugsYkDao;
import cn.gson.springboot.model.dao.FankuDAO;
import cn.gson.springboot.model.dao.FankuDetaiDAO;
import cn.gson.springboot.model.mapper.CkMapper;
import cn.gson.springboot.model.pojos.Apply;
import cn.gson.springboot.model.pojos.Applydetai;
import cn.gson.springboot.model.pojos.Ck;
import cn.gson.springboot.model.pojos.DrugsYk;
import cn.gson.springboot.model.pojos.Fanku;
import cn.gson.springboot.model.pojos.Fankudetai;
import cn.gson.springboot.model.vo.FkdrugsVO;
import cn.gson.springboot.model.vo.SqdrugsVO;

@Service
@Transactional(rollbackFor=Exception.class)
public class CkService {
	@Autowired
	private CkMapper tm;
	@Autowired
	private ApplyDAO od;
	@Autowired
	private ApplyDetailDAO odd;
	@Autowired
	private DrugsYkDao ddao;
	
	@Autowired
	private CkDAO ck;
	
	@Autowired
	private FankuDAO fdao;
	@Autowired
	private FankuDetaiDAO fddao;
	
	//查询所有药品
	public List<Map<String,Object>> cxsy(String aa) {
		List<Map<String,Object>> ll= tm.cxsy(aa); 
		return ll;
	}
	//查询所有药品
		public List<Map<String,Object>> cxblyp() {
			List<Map<String,Object>> ll= tm.cxblyp(); 
			return ll;
		}
		//模糊查询申请药品
				public List<Map<String,Object>> mhcxblyp(String aa) {
					List<Map<String,Object>> ll= tm.mhcxblyp(aa); 
					return ll;
				}
	//申请表主从表添加
	public void saveOrder(SqdrugsVO svo) {
		//获取采购订单进行新增
		Apply order = svo.getOrder();
		
		Apply save = od.save(order);
		
		List<Applydetai> details = svo.getDetails();
		//循环
		for (Applydetai pur : details) {
			//添加订单信息
			pur.setApply (order); 
			//查询药品
			DrugsYk drug = ddao.findById(pur.getDrugsYk().getDrugId()).get();
			//重新赋值一下药品对象
			pur.setDrugsYk(drug);
			odd.save(pur);
		}
		//pddao.saveAll(details);
	}
	//反库表主从表添加
		public void fankuOrder(FkdrugsVO svo) {
			//获取采购订单进行新增
			Fanku order = svo.getFk();
			
			Fanku save = fdao.save(order);
			
			List<Fankudetai> details = svo.getFkdetails();
			//循环
			for (Fankudetai pur : details) {
				//添反库表详情信息
				pur.setFanku(order);
				int id=pur.getDrugsYk().getDrugId();
				System.out.println("id是"+id);
				
				Ck c=ck.kmpccx(id,pur.getYppc());
				System.out.println(c.getDrugs_Stock_kc()+"--------");
				c.setDrugs_Stock_kc(0);
				ck.save(c);
				System.out.println("修改成功");
				
				
				//查询药品
				DrugsYk drug = ddao.findById(pur.getDrugsYk().getDrugId()).get();

				//重新赋值一下药品对象
				pur.setDrugsYk(drug);
				fddao.save(pur);
			}
			//pddao.saveAll(details);
		}
}
